SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE PROCEDURE [dbo].[wp_Actualizar_Articulo_Clasificacion]
	@id_articulo AS _codigo,
	@clasificaciones AS VARCHAR(MAX)
AS


DECLARE @ArtClas AS TABLE(
	id_clasificacion CHAR(10),
	valor CHAR(50)
)

INSERT @ArtClas
SELECT SUBSTRING(value, 0, CHARINDEX('~', value)), SUBSTRING(value,  CHARINDEX('~', value)+1, LEN(value))
FROM dbo.fnSplitString(@clasificaciones, '£')


DELETE FROM dbo.App_articulo_clasificacion
WHERE id_articulo = @id_articulo AND
		id_clasificacion NOT IN (SELECT id_clasificacion
								FROM @ArtClas)
			
								
UPDATE AAC
SET AAC.valor = AC.valor
FROM dbo.app_articulo_clasificacion AAC 
INNER JOIN @ArtClas AC ON AAC.id_articulo = @id_articulo AND AAC.id_clasificacion = AC.id_clasificacion


INSERT App_articulo_clasificacion
SELECT @id_articulo, id_clasificacion, valor
FROM @ArtClas AC
WHERE id_clasificacion NOT IN (SELECT id_clasificacion
								FROM App_articulo_clasificacion
								WHERE id_articulo = @id_articulo)
GO
